package la.bbs558.strategy.send;

import la.bbs558.entity.dto.EmailMap;
import la.bbs558.entity.enums.CaptchaTypeEnum;
import la.bbs558.strategy.base.StrategyEnum;
import la.bbs558.util.EmailUtils;
import la.bbs558.util.ExecutorManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @author: Shanks
 * @date: 2024/1/25 14:18
 * @desc:
 */
@Slf4j
@Component
public class MailSendStrategy extends AbstractSendStrategy {
    private static final String BBS_NAME = "558出海社区";

    @Override
    public StrategyEnum getStrategyEnum() {
        return CaptchaTypeEnum.MAIL;
    }

    @Override
    protected void doHandle(SendContext context) {
        String email = context.getTo();
        String type = context.getServerTypeEnum().getDesc();
        long time = context.getTime();
        TimeUnit timeUnit = context.getTimeUnit();
        long minutes = timeUnit.toMinutes(time);
        // 校验邮箱
        String code = context.getCode();
        // 发邮件
        EmailMap emailMap = new EmailMap(
                email, BBS_NAME + type, BBS_NAME, type, "尊敬的" + BBS_NAME + "用户", code, minutes + "分钟");
        //异步发送邮件
        ExecutorManager.myExecutor.execute(() -> EmailUtils.send(emailMap));
    }
}
